Micron Document
`:top
In `F33f`_`[ADO.NET`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=ADO.NET]`_`f, a `!DataReader`! is a broad category of objects used to sequentially read data from a data source.`:cite-ref-datareaders-1-0[`F5bf`_`[1`#cite-note-datareaders-1]`_`f] DataReaders provide a very efficient way to access data, and can be thought of as a Firehose cursor from `F33f`_`[ASP Classic`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Active_Server_Pages]`_`f, except that no `F33f`_`[server-side`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Server-side]`_`f `F33f`_`[cursor`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Cursor_(databases)]`_`f is used. A DataReader parses a `F33f`_`[Tabular Data Stream`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Tabular_Data_Stream]`_`f from `F33f`_`[Microsoft SQL Server`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Microsoft_SQL_Server]`_`f, and other methods of retrieving data from other sources.

A DataReader is usually accompanied by a Command object that contains the query, optionally any parameters, and the connection object to run the query on.

>>Contents

• `F0af`_`[DataReader types`#datareader-types]`_`f
• `F0af`_`[Strong vs weak typing`#strong-vs-weak-typing]`_`f
• `F0af`_`[Common errors`#common-errors]`_`f
• `F0af`_`[Sample code`#sample-code]`_`f
• `F0af`_`[References`#references]`_`f

-─

>>DataReader types

There is no DataReader class in `F33f`_`[ADO.NET`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=ADO.NET]`_`f, but there are a number of classes that implement the IDataReader interface:

• System.Data.SqlClient.SqlDataReader
• System.Data.OleDb.OleDbDataReader
• Oracle.OracleClient.OracleDataReader

DataReaders have a small footprint and good performance because each is tailor-made to the task at hand, however this makes it more difficult to write an application that can be moved from one backend data source to another. Some provider-specific DataReaders expose types used by the underlying database - for example, int values can be null in Microsoft SQL Server, but not in the `F33f`_`[.NET Framework`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=.NET_Framework]`_`f prior to version 2.0.

>>Strong vs weak typing

When using a DataReader to retrieve data, the developer can choose to read field values in strongly typed manner ( `*example: `B100`F9d9myReader.GetString(12)`f`b`* ) or a weakly typed manner, returning then as System.Objects ( `*example:`* `B100`F9d9myReader.GetValue(12)`f`b ). Both approaches have their pros and cons.

Using the strongly typed retrieval methods can be more cumbersome, especially without specific knowledge of the underlying data. Numeric values in the database can translate to several `F33f`_`[.NET`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=.NET_Framework]`_`f types: `F33f`_`[Int16`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Int16]`_`f, `F33f`_`[Int32`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Int32]`_`f, `F33f`_`[Int64`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Int64]`_`f, Float, `F33f`_`[Decimal`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Decimal]`_`f, or `F33f`_`[Currency`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Currency]`_`f. Trying to retrieve a value using the wrong type results in an exception being thrown, which stops code from running further, and slows the application down. This is also true when you use the right type, but encounter a DbNull value ( `*this can be avoided by using the IsDbNull boolean function of the DataReader class`* ). The benefit to this retrieval method is that data validation is performed sooner, improving the probability of data correction being possible.

Weakly typed data retrieval allows for quick code writing, and allows for the data to be used in some fashion when the developer doesn't know beforehand what types will be returned. Further, with some effort, the programmer can extract the value into a variable of the proper type by using the GetFieldType or GetDataTypeName methods of the DataReader.

>>Common errors

A DataReader can in some cases be used in place of a DataTable, however many programmers have experienced connection bloat when following this approach. A DataReader can only be used against an (already) open `F33f`_`[database connection`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Database_Connection]`_`f; that connection isn't closed until the DataReader's Dispose method is called. If an exception is thrown while the data is being processed, for example as described in `*`F33f`_`[Strong and weak typing`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Strong_and_weak_typing]`_`f`*, above, the Dispose method will never be called if the developer writes code explicitly declaring and disposing the DataReader without the use of a try-finally block. The `F33f`_`[C#`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=C_Sharp_(programming_language)]`_`f using construct is a good way to avoid this problem, as shown below in the code example.

>>Sample code

Sample of accessing SQL Data using DataReader

`B100`F9d9void DataTest()`f`b
`B100`F9d9{`f`b
`B100`F9d9 using (SqlConnection conn1 = new SqlConnection(...))`f`b
`B100`F9d9 {`f`b
`B100`F9d9 conn1.Open();`f`b
`B100`F9d9 SqlCommand mycommand = new SqlCommand("select * from someTable", conn1);`f`b
`B100`F9d9 using (SqlDataReader myreader = mycommand.ExecuteReader())`f`b
`B100`F9d9 {`f`b
`B100`F9d9 if (myreader != null)`f`b
`B100`F9d9 while (myreader.Read())`f`b
`B100`F9d9 Console.WriteLine(myreader.GetValue(0).ToString() + ":" + myreader.GetTypeName(0));`f`b
`B100`F9d9`f`b
`B100`F9d9 }`f`b
`B100`F9d9 mycommand.Dispose();`f`b
`B100`F9d9 }`f`b
`B100`F9d9}`f`b

`B100`F9d9using System;`f`b
`B100`F9d9using System.Collections.Generic;`f`b
`B100`F9d9using System.Linq;`f`b
`B100`F9d9using System.Text;`f`b
`B100`F9d9using System.Data.Odbc;`f`b
`B100`F9d9using MySql.Data.MySqlClient;`f`b
`B100`F9d9`f`b
`B100`F9d9namespace ConsoleApplication1;`f`b
`B100`F9d9`f`b
`B100`F9d9class Program`f`b
`B100`F9d9{`f`b
`B100`F9d9 static void Main(string[] args)`f`b
`B100`F9d9 {`f`b
`B100`F9d9 string Conn = "Server=localhost;Uid=root;Pwd=thiru;Database=Employee";`f`b
`B100`F9d9 MySql.Data.MySqlClient.MySqlConnection conn = new MySql.Data.MySqlClient.MySqlConnection(Conn);`f`b
`B100`F9d9`f`b
`B100`F9d9 MySqlCommand comm = new MySqlCommand("select * from emp", conn);`f`b
`B100`F9d9 conn.Open();`f`b
`B100`F9d9`f`b
`B100`F9d9 MySqlDataReader Ada = comm.ExecuteReader();`f`b
`B100`F9d9 while (Ada.Read())`f`b
`B100`F9d9 {`f`b
`B100`F9d9 Console.WriteLine(Ada[0]);`f`b
`B100`F9d9 }`f`b
`B100`F9d9 }`f`b
`B100`F9d9}`f`b

>>References

`:cite-note-datareaders-1`!1.`! `F0af`_`[↑`#cite-ref-datareaders-1-0]`_`f "DataAdapters and DataReaders". `*docs.microsoft.com`*. Microsoft. Retrieved 4 September 2017.

`:citereffoggon2006`aFoggon, Damien (2006). "DataReader and DataSet". `*Beginning ASP.NET 2.0 Databases: From Novice to Professional`*. Apress. pp. 155–187. `F33f`_`[doi`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Doi_(identifier)]`_`f:10.1007/978-1-4302-0146-5_5. `F33f`_`[ISBN`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=ISBN_(identifier)]`_`f 978-1-4302-0146-5. Retrieved 22 March 2025.

`:citerefpolli2004`aPolli, Andrea (10 October 2004). "DATAREADER: A tool for art and science collaborations". `*Proceedings of the 12th annual ACM international conference on Multimedia`*. Association for Computing Machinery. pp. 520–523. `F33f`_`[doi`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Doi_(identifier)]`_`f:10.1145/1027527.1027654. `F33f`_`[ISBN`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=ISBN_(identifier)]`_`f 1-58113-893-8. Retrieved 22 March 2025.

`c`F0af`_`[↑ Back to top`#top]`_`f`a